import 'package:flustars/flustars.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:zrjz/comm/base/base.dart';
import 'package:zrjz/comm/constants/color_constants.dart';
import 'package:zrjz/comm/constants/constants.dart';
import 'package:zrjz/comm/utils/ex_widget.dart';
import 'package:zrjz/comm/utils/ext_dart.dart';
import 'package:zrjz/comm/utils/log_utils.dart';
import 'package:zrjz/comm/widget/common_widget.dart';
import 'package:zrjz/comm/widget/double_tap_back_exit_app.dart';
import 'package:zrjz/comm/widget/my_button.dart';
import 'package:zrjz/local/theme/theme_config.dart';
import 'package:zrjz/modules/auth/login/login_page.dart';
import 'package:zrjz/modules/genericity/box.dart';
import 'package:zrjz/modules/genericity/icompress.dart';
import 'package:zrjz/modules/genericity/my_class.dart';
import 'package:zrjz/modules/main/main_state.dart';
import 'package:zrjz/modules/me/form_filed/form_filed_page.dart';
import 'package:zrjz/modules/me/setting/setting_page.dart';
import 'package:zrjz/router/page_router.dart';

import 'main_controller.dart';

/*
   App首页页面
   底部5个 Tab + PageView 切换
 */
class MainPage extends BaseStatefulPage<MainController> {
  MainPage({super.key});

  //启动当前页面
  static void startInstance() {
    return Get.start(RouterPath.MAIN, launchModel: LaunchModel.singleTask);
  }

  static void startWithPopAll() {
    Get.offAllNamed(RouterPath.MAIN);
  }

  @override
  State<MainPage> createState() => _MainPageState();

  @override
  MainController createRawController() {
    return MainController();
  }
}

class _MainPageState extends BaseState<MainPage, MainController> with StateLifecycle {
  late MainState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
  }

  @override
  void dispose() {
    Get.delete<MainController>();
    super.dispose();
  }

  @override
  void onResume() {
    super.onResume();
  }

  //黑暗模式的切换监听
  @override
  void didChangePlatformBrightness() {
    int? darkModel = SpUtil.getInt(AppConstant.storagedarkmodel, defValue: 0);
    //当跟随系统的时候，监听系统当前的黑暗模式
    if (darkModel == 0) {
      //延时拿到当前的值，否则可能获取的值不对
      Future.delayed(const Duration(milliseconds: 250), () {
        Brightness currentBrightness = MediaQuery.of(context).platformBrightness;
        //切换全部的Theme并刷新
        if (currentBrightness == Brightness.dark) {
          ThemeConfig.changeThemeDark();
        } else {
          ThemeConfig.changeThemeLight();
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    //双击退出应用
    return DoubleTapBackExitApp(
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        value: Get.isDarkMode ? ThemeConfig.systemUiOverlayStyleDarkTheme : ThemeConfig.systemUiOverlayStyleLightThemeWhite,
        child: autoCtlGetBuilder(builder: (controller) {
          return Scaffold(
            appBar: CommonWidget.appBar(context, "首页".tr, backCallback: () {
              SmartDialog.showToast("你别点我了，没用！");
            }),
            body: SafeArea(
              bottom: true,
              top: false,
              //真正的 Content 布局,使用PageView保存状态
              child: Scrollbar(
                  child: SingleChildScrollView(
                child: Column(mainAxisSize: MainAxisSize.min, children: [
                  MyButton(
                    onPressed: () {
                      LoginPage.startInstance(LaunchModel.singleTop);
                    },
                    text: "去登陆页面",
                    minWidth: 150,
                    fontSize: 15,
                    backgroundColor: ColorConstants.appBlue,
                    textColor: ColorConstants.white,
                    elevation: 3,
                  ).center().marginOnly(top: 10),
                  MyButton(
                    onPressed: () {
                      SettingPage.startInstance();
                    },
                    text: "去设置页面",
                    minWidth: 150,
                    fontSize: 15,
                    backgroundColor: ColorConstants.appBlue,
                    textColor: ColorConstants.white,
                    elevation: 3,
                  ).center().marginOnly(top: 10),
                  MyButton(
                    onPressed: () {
                      FormFiledPage.startInstance();
                    },
                    text: "Form表单页面",
                    minWidth: 150,
                    fontSize: 15,
                    backgroundColor: ColorConstants.appBlue,
                    textColor: ColorConstants.white,
                    elevation: 3,
                  ).center().marginOnly(top: 10),
                  MyButton(
                    onPressed: () {
                      // var box = Box<MyClass>(() => MyClass());
                      // var myClassInstance = box.create();
                      // Log.d("myClassInstance:$myClassInstance");

                      var test = NetTest();
                      test.setCompressDsl((c) {
                        c.OnStart(() => {Log.d("开始")});

                        c.OnSuccess((x, y) => {Log.d("x:$x y:$y")});

                        c.OnError((e) => {Log.d("e:$e")});
                      });
                      test.start(onSuccess: (x, y) {
                        Log.d("x:$x y:$y");
                      }, onError: (e) {
                        Log.d("e:$e");
                      });
                    },
                    text: "Dart函数",
                    minWidth: 150,
                    fontSize: 15,
                    backgroundColor: ColorConstants.appBlue,
                    textColor: ColorConstants.white,
                    elevation: 3,
                  ).center().marginOnly(top: 10),
                ]),
              )),
            ),
          );
        }),
      ),
    );
  }
}
